package top.ewind.controller.wx;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.jfinal.aop.Before;
import com.jfinal.core.AuthorAnnoInterceptor;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import top.ewind.controller.common.WxRootCtrl;
import top.ewind.model.SysDictory;
import top.ewind.util.SecurityUtil;

public class WxCtrl extends WxRootCtrl {
	/**
	 * 
	 * 微信首页页面
	 * **/
	@Before(AuthorAnnoInterceptor.class)
	public void index() {
		String mac = getPara("mac");
		Integer nid = getSessionAttr("nid");
		Record user = getSessionAttr("sysuser");
		String userid = user.getStr("id");
		List<Record> remind =  Db.find("select * from device_remind where mac = ? and nid = ? order by intime desc limit 50",mac,nid);
		List<Record> devicebinds =  Db.find("select * from device_bind where userid = ? and nid = ?",userid,nid);
		Record deviceb = Db.findFirst("select deviceid from device_bind where userid = ? and nid = ?",userid,nid);
		if(mac==null){
			if(deviceb!=null){
				mac = deviceb.getStr("deviceid");
			}
			
		}
		Record deviceset = Db.findFirst("select * from device_set where userid = ? and mac = ?",userid,mac);
		setAttr("deviceset",deviceset);
		setAttr("reminds",remind);
		setAttr("devicebinds",devicebinds);
		setAttr("devicebindss",devicebinds.size());
		setAttr("mqttserver",SysDictory.getConfig("mqttserver"));
		String openid = getSessionAttr("openid-"+nid);
		setAttr("userid",userid);
		long timestamp = System.currentTimeMillis();
		String userkey = "jfajskfjsla";
		if(StrKit.notBlank(getPara("did"))){
			setAttr("seldid",getPara("did"));
		}
		setAttr("dpass",SecurityUtil.md5(userkey+timestamp)+","+timestamp);
		if(StringUtils.isNotEmpty(mac)){
			String pid = Db.queryStr("select pid from device where id=?", mac);
			setAttr("mac", mac);
			setAttr("pid", pid);
		}
		setAttr("mac", mac);		
		
		render("/aigo/index.html");
	}
	/**
	 * 
	 * 空气等级
	 * **/
	public void dj() {
		
		render("/aigo/dj.html");
	}
	/**
	 * 
	 * 设备自检
	 * **/
	public void dtest() {
		
		render("/aigo/devicetest.html");
	}
	/**
	 * 
	 * 分享设备
	 * **/
	public void shadivce() {
		
		render("/aigo/devicesha.html");
	}
	/**
	 * 
	 * 指标等级
	 * **/
	public void djs() {
		int types = Integer.parseInt(getPara());
		String titleval = null;
		String mac = getPara("mac");
		String uesrid = getPara("userid");
		Record deviceset =  Db.findFirst("select * from device_set where mac = ? and userid = ?",mac,uesrid);
		Map<String, Object> mess = new HashMap<String, Object>();
		if(types==0){
			mess.put("l", "甲烷，丙烷含量极低，处于正常状态，无危险");
			mess.put("ls", "甲烷，丙烷有泄漏，易发生危险火灾");
			mess.put("lss", "甲烷，丙烷严重泄漏，易发生爆炸");
			mess.put("low", deviceset.getInt("krlow")+"-"+deviceset.getInt("krlowd")+"(%LEL)");
			mess.put("lows", deviceset.getInt("krlows")+"-"+deviceset.getInt("krlowsd")+"(%LEL)");
			mess.put("lowss", deviceset.getInt("krlowss")+"-"+deviceset.getInt("krlowssd")+"(%LEL)");
			titleval = "可燃气体指标等级";
		}
		if(types==1){
			mess.put("l", "co2含量极低，处于正常状态，无危险");
			mess.put("ls", "co2有泄漏，易发生中毒危险");
			mess.put("lss", "co2严重泄漏，易发生中毒危险");
			mess.put("low", deviceset.getInt("ydlow")+"-"+deviceset.getInt("ydlowd")+"(PPM)");
			mess.put("lows", deviceset.getInt("ydlows")+"-"+deviceset.getInt("ydlowsd")+"(PPM)");
			mess.put("lowss", deviceset.getInt("ydlowss")+"-"+deviceset.getInt("ydlowssd")+"(PPM)");
			titleval = "有毒气体指标等级";
		}
		setAttr("mess",mess);		
		setAttr("titleval",titleval);
		render("/aigo/djs.html");
	}
	/**
	 * 
	 * 设备列表
	 * **/
	public void devicelist() {
		String openid = getPara();
		Record userlist = Db.findFirst("select * from device_user where openid = ?",openid);
		List<Record> devicebindlist = Db.find("select * from device_bind where userid = ?",userlist.getStr("id"));
		setAttr("devicebindlist",devicebindlist);
		render("/aigo/devicelist.html");
	}
	/**
	 * 
	 * 设备详情设置
	 * **/
	public void deviceset() {
		String bindid = getPara();
		Record bindtable = Db.findById("device_bind", bindid);
		Record deviceset = Db.findFirst("select * from device_set where mac = ?", bindtable.getStr("deviceid"));
		setAttr("dname",bindtable);
		setAttr("deviceset",deviceset);
		render("/aigo/devicedetail.html");
	}
	/**
	 * 
	 * 删除绑定设备
	 * **/
	public void deletbind(){
		String devicebindid = getPara();
		boolean ret = false;
		ret = Db.deleteById("device_bind", devicebindid);
		if(ret){
			renderJson("msg", "1");
		}else{
			renderJson("msg", "0");
		}
	}
	/**
	 * 
	 * 保存设置
	 * **/
	public void sava() {
		boolean ret = false;
		String devicebindid = getPara();
		Record bindtable = Db.findById("device_bind", devicebindid);
		if(bindtable!=null){
			bindtable.set("udname", getPara("devicename"));
			Db.update("device_bind",bindtable);
			Record deviceset = Db.findFirst("select * from device_set where mac = ?", bindtable.getStr("deviceid"));
			if(deviceset!=null){
				deviceset.set("krlow", getPara("krlow")).set("krlowd", getPara("krlowd")).set("krlows", getPara("krlows")).set("krlowsd", getPara("krlowsd")).set("krlowss", getPara("krlowss")).set("krlowssd", getPara("krlowssd")).set("ydlow", getPara("ydlow")).set("ydlowd", getPara("ydlowd")).set("ydlows", getPara("ydlows")).set("ydlowsd", getPara("ydlowsd")).set("ydlowss", getPara("ydlowss")).set("ydlowssd", getPara("ydlowssd"));
				ret = Db.update("device_set",deviceset);
			}else{
				Record devicesets =new Record().set("mac", bindtable.getStr("deviceid")).set("userid", bindtable.getStr("userid")).set("krlow", getPara("krlow")).set("krlowd", getPara("krlowd")).set("krlows", getPara("krlows")).set("krlowsd", getPara("krlowsd")).set("krlowss", getPara("krlowss")).set("krlowssd", getPara("krlowssd")).set("ydlow", getPara("ydlow")).set("ydlowd", getPara("ydlowd")).set("ydlows", getPara("ydlows")).set("ydlowsd", getPara("ydlowsd")).set("ydlowss", getPara("ydlowss")).set("ydlowssd", getPara("ydlowssd"));
				ret = Db.save("device_set", devicesets);
			}
		}
		if(ret){
			renderJson("msg", "success");
		}else{
			renderJson("msg", "保存失败！");
		}
		
	}
}
